package com.example.tests;

import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactCreateTests extends TestBase {
		
	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException{
		return wrapContactsForDataProvider(loadContactsFromXmlFile(new File("contact.xml"))).iterator();
	}
	
	@Test(dataProvider = "contactsFromFile")
	public void testContactCreationWhitValidData(ContactData contact) throws Exception {				
		// save old action		
		SortedListOf<ContactData> oldList = app.getModel().getContacts();
		
		// actions	
		app.getContactHelper().createContact(contact);
		
		// save new actions
		SortedListOf<ContactData> newList = app.getModel().getContacts();
		
		// compare states	 
		assertThat(newList, equalTo(oldList.withAdded(contact)));	
		
		if(wantToCheck()){
			if("yes".equals(app.getProperty("check.db"))){			
			    assertThat(app.getModel().getContacts(), equalTo(app.getHibernateHelper().listContact()));
			}
			
			if("yes".equals(app.getProperty("check.ui"))){
			    assertThat(app.getModel().getContacts(), equalTo(app.getContactHelper().getUiContacts()));
		    }			
	    }
	}
}
